from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

from cfg import *
import time


class SMP_UI:

    def __init__(self):
        options = webdriver.ChromeOptions()
        options.add_experimental_option(
            'excludeSwitches', ['enable-logging']
        )
        self.wd = webdriver.Chrome(options=options)
        self.wd.implicitly_wait(10)

    def login(self, username, password):
        self.wd.get(SMP_URL_LOGIN)
        time.sleep(1)
        if username is not None:
            self.wd.find_element(By.ID, "username").send_keys(username)  # 输入用户名
        time.sleep(1)
        if password is not None:
            self.wd.find_element(By.ID, "password").send_keys(password)  # 输入密码
        time.sleep(1)
        self.wd.find_element(By.ID, "loginBtn").click()  # 点击登录

    def add_device_model(self, devtype, name, desc):  # 设备管理界面，添加设备数据

        time.sleep(1)
        # 定位到select元素
        select_element = smpui.wd.find_element(By.XPATH, "/html/body/main/div[1]/div/div[1]/select")
        # 创建Select对象
        select = Select(select_element)
        # 通过value属性选择选项
        select.select_by_visible_text(devtype)

        smpui.wd.find_element(By.XPATH, '//*[@id="device-model"]').send_keys(name)

        smpui.wd.find_element(By.XPATH, '//*[@id="device-model-desc"]').send_keys(desc)

        smpui.wd.find_element(By.XPATH, '/html/body/main/div[1]/div/div[4]/span').click()

    def get_first_page(self):  # 设备管理界面，查看存在列表的第一项
        time.sleep(1)
        self.wd.implicitly_wait(10)
        values = self.wd.find_element(By.XPATH, '/html/body/main/div[3]/div/div[1]')
        self.wd.implicitly_wait(10)
        result = []
        item = values.find_elements(By.CLASS_NAME, "field-value")
        for i in item:
            result.append(i.text)
        return result

    def del_first_item(self) -> bool:  # 设备管理界面，删除列表中的第一个
        self.wd.implicitly_wait(0)
        delBtn = self.wd.find_element(By.XPATH, "/html/body/main/div[3]/div/div[2]/span[1]")
        self.wd.implicitly_wait(10)
        if not delBtn:
            return False
        delBtn.click()
        self.wd.switch_to.alert.accept()
        return True

    def get_first_item_rule(self):  # 业务规则的查看存在列表的第一项
        time.sleep(1)
        self.wd.implicitly_wait(10)
        values = self.wd.find_element(By.XPATH, '/html/body/main/div[3]/div/div[1]')
        self.wd.implicitly_wait(10)
        result = []
        data = values.find_elements(By.CLASS_NAME, "field-value")
        for i in data:
            result.append(i.text)
        results = [val.replace("\n", "") for val in result]
        return results

    def add_eqment(self, devtype, model, rule, num, desc):  # 设备管理界面，添加设备数据
        time.sleep(1)
        # 定位到select元素
        select_element1 = smpui.wd.find_element(By.XPATH, "/html/body/main/div[1]/div/div[1]/select")
        # 创建Select对象
        select1 = Select(select_element1)
        # 选择选项
        select1.select_by_visible_text(devtype)
        select_element2 = smpui.wd.find_element(By.XPATH, "/html/body/main/div[1]/div/div[2]/select")
        # 创建Select对象
        select2 = Select(select_element2)
        # 选择选项
        select2.select_by_visible_text(model)
        select_element3 = smpui.wd.find_element(By.XPATH, "/html/body/main/div[1]/div/div[3]/select")
        # 创建Select对象
        select3 = Select(select_element3)
        # 选择选项
        select3.select_by_visible_text(rule)
        smpui.wd.find_element(By.XPATH, '/html/body/main/div[1]/div/div[4]/input').send_keys(num)
        smpui.wd.find_element(By.XPATH, '/html/body/main/div[1]/div/div[5]/input').send_keys(desc)
        smpui.wd.find_element(By.XPATH, '/html/body/main/div[1]/div/div[6]/span').click()


smpui = SMP_UI()
